import { Context, Next } from "koa"
import { IResData, IResDataBase } from "../data/ResData"

async function result(ctx: Context, next: Next) {
  // response result fun
  ctx.result = (res: IResData) => {
    ctx.status = 200
    ctx.body = {
      code: res.code,
      data: res.data,
      message: res.message   // 多用于错误信息
    }
  }
  // success result fun
  ctx.succ = (res: IResDataBase) => {
    ctx.result({
      code: 200,
      data: res.data,
      message: res.message
    })
  }
  // error result fun
  ctx.err = (res: IResDataBase) => {
    ctx.result({
      code: 500,
      data: res.data,
      message: res.message
    })
  }

  await next()
}

export default result